package com.liao.starapp.star;

import android.annotation.SuppressLint;
import android.graphics.Bitmap;
import android.os.Bundle;
import android.view.View;
import android.view.Window;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.appcompat.app.ActionBar;
import androidx.appcompat.app.AppCompatActivity;
import androidx.viewpager.widget.ViewPager;

import com.liao.starapp.R;
import com.liao.starapp.utils.GetAssetsData;

import java.io.IOException;
import java.util.ArrayList;

public class StarDiagramActivity extends AppCompatActivity implements View.OnClickListener {

    private ImageView img_back;
    private TextView tv_title;
    private ViewPager viewPager;

    private ArrayList<View> views;
    private String[] name = new String[]{"白羊座", "金牛座", "双子座", "巨蟹座", "狮子座", "处女座"
            , "天秤座", "天蝎座", "射手座", "摩羯座", "水瓶座", "双鱼座"};

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_star_diagram);
        ActionBar actionBar = getSupportActionBar();
        if (actionBar != null) {
            actionBar.hide();
        }
        Window window = getWindow();
        window.setStatusBarColor(getColor(R.color.actionBarColor));
        initView();
    }

    private void initView() {
        img_back = (ImageView) findViewById(R.id.img_back);
        tv_title = (TextView) findViewById(R.id.tv_title);
        viewPager = (ViewPager) findViewById(R.id.viewPager);

        img_back.setOnClickListener(this);

        views = new ArrayList<>();
        ArrayList<Bitmap> bitmaps = getBitmap();
        for (Bitmap bitmap : bitmaps) {
            ImageView imageView = new ImageView(this);
            imageView.setImageBitmap(bitmap);
            views.add(imageView);
        }
        StarDiagramViewPagerAdapter adapter = new StarDiagramViewPagerAdapter(views);
        viewPager.setAdapter(adapter);
        viewPager.addOnPageChangeListener(new ViewPager.SimpleOnPageChangeListener() {
            @SuppressLint("SetTextI18n")
            @Override
            public void onPageSelected(int position) {
                tv_title.setText(name[position] + "（" + (position + 1) + "/" + views.size() + "）");
            }
        });
    }

    private ArrayList<Bitmap> getBitmap() {
        ArrayList<Bitmap> bitmaps = new ArrayList<>();
        try {
            String[] xzps = getAssets().list("xzp");
            for (String xzp : xzps) {
                Bitmap bitmap = GetAssetsData.getGridViewImg(this, "xzp/" + xzp);
                bitmaps.add(bitmap);
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
        return bitmaps;
    }

    @Override
    public void onClick(View v) {
        onBackPressed();
        finish();
    }
}
